package com.blog4j.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.blog4j.common.model.Result;
import com.blog4j.common.utils.CommonUtil;
import com.blog4j.system.entity.WebInfoEntity;
import com.blog4j.system.service.NoticeService;
import com.blog4j.system.service.SystemService;
import com.blog4j.system.service.WebInfoService;
import com.blog4j.system.vo.req.CreateNoticeReqVo;
import com.blog4j.system.vo.req.EditNoticeReqVo;
import com.blog4j.system.vo.req.SaveSystemReqVo;
import com.blog4j.system.vo.req.SaveWebInfoReqVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/17 12:50
 **/
@RestController
@RequestMapping("/")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SystemController {
    private final SystemService systemService;
    private final NoticeService noticeService;
    private final WebInfoService webInfoService;

    /**
     * 保存系统配置信息
     *
     * @param reqVo 请求信息
     * @return 保存成功
     */
    @SaCheckRole(value = "SUPER_ADMIN")
    @PostMapping("/saveBaseSystemConfig")
    public Result saveBaseSystemConfig(@RequestBody @Valid SaveSystemReqVo reqVo) {
        systemService.saveBaseSystemConfig(reqVo);
        return Result.ok();
    }

    /**
     * 保存网站配置信息
     *
     * @param reqVo 请求信息
     * @return 保存成功
     */
    @SaCheckRole(value = "SUPER_ADMIN")
    @PostMapping("/saveWebInfo")
    public Result saveWebInfo(@RequestBody @Valid SaveWebInfoReqVo reqVo) {
        WebInfoEntity webInfo = new WebInfoEntity();
        BeanUtils.copyProperties(reqVo, webInfo);
        webInfo.setUpdateTime(CommonUtil.getCurrentDateTime());
        webInfoService.updateById(webInfo);
        return Result.ok();
    }

    /**
     * 编辑公告
     *
     * @param reqVo 请求信息
     * @return 编辑成功
     */
    @SaCheckRole(value = {"SUPER_ADMIN", "ORGANIZATION_ADMIN"}, mode = SaMode.OR)
    @PostMapping("/editNotice")
    public Result editNotice(@RequestBody @Valid EditNoticeReqVo reqVo) {
        noticeService.editNotice(reqVo);
        return Result.ok();
    }

    /**
     * 删除公告
     *
     * @param id 公告ID
     * @return 删除成功
     */
    @SaCheckRole(value = {"SUPER_ADMIN", "ORGANIZATION_ADMIN"}, mode = SaMode.OR)
    @GetMapping("/delNotice/{id}")
    public Result delNotice(@PathVariable("id") Integer id) {
        noticeService.delNotice(id);
        return Result.ok();
    }

    /**
     * 创建公告
     *
     * @param reqVo 请求信息
     * @return 创建成功
     */
    @SaCheckRole(value = {"SUPER_ADMIN", "ORGANIZATION_ADMIN"}, mode = SaMode.OR)
    @PostMapping("/createNotice")
    public Result createNotice(@RequestBody @Valid CreateNoticeReqVo reqVo) {
        noticeService.createNotice(reqVo);
        return Result.ok();
    }
}
